// index.js
Page({
  data: {
    searchKey: '',
    total: 50,
    signed: 20,
    currentPage: 0,
    pageOptions: ['', '1', '2', '3', '4', '5'],
    showAdd: false,
    newUser: { phone: '', name: '', order: '' },
    userList: [
      // 测试数据
      {
        id: 1,
        phone: '13800138000',
        name: '张三',
        signed: true,
        time: '09:00'
      },
      {
        id: 2,
        phone: '13900139000',
        name: '李四',
        signed: false,
        time: ''
      }
    ]
  },

  handleSearch() {
    wx.showToast({
      title: '搜索中...',
      icon: 'loading'
    });

    wx.request({
      url: 'http://124.221.243.113:1221/app/getNoSignIn',
      method: 'GET',
      success: (res) => {
        console.log(res); // 打印res结果到控制台

        if (res.data.code === 200) {
          const noSignInPhones = res.data.data;
          this.setData({
            userList: noSignInPhones.map(phone => ({
              phone,
              name: '未知',
              signed: false,
              time: ''
            }))
          });
          wx.showModal({
            title: '查询结果',
            content: `未签到手机号：\n${noSignInPhones.join('\n')}`,
            showCancel: false,
            confirmText: '确定'
          });
        } else {
          wx.showModal({
            title: '错误',
            content: `查询失败：${res.data.msg || '未知错误'}`,
            showCancel: false,
            confirmText: '确定'
          });
        }
      },
      fail: (err) => {
        console.log(err); // 打印错误信息到控制台
        wx.showModal({
          title: '请求失败',
          content: '无法连接到服务器，请稍后重试。',
          showCancel: false,
          confirmText: '确定'
        });
      }
    });
  },

  handlePageChange(e) {
    this.setData({ currentPage: e.detail.value });
    this.handleSearch();
  },

  copyPhone(e) {
    const phone = e.currentTarget.dataset.phone;
    wx.setClipboardData({
      data: phone,
      success() {
        wx.showToast({
          title: '手机号已复制',
          icon: 'success'
        });
      }
    });
  },

  showAddDialog() {
    this.setData({ showAdd: true });
  },

  hideAddDialog() {
    this.setData({ showAdd: false });
  },

  handleAddUser() {
    const { phone, name, order } = this.data.newUser;
    if (!phone || !name || !order) {
      wx.showToast({ title: '请填写完整信息', icon: 'none' });
      return;
    }
    
    // TODO: 对接新增接口
    this.hideAddDialog();
    wx.showToast({ title: '添加成功' });
    this.setData({
      newUser: { phone: '', name: '', order: '' }
    });
  }
});
